
package com.jf.cloud.multishop.controller.app;

import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.multishop.vo.ShopDetailVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.multishop.constant.RenovationType;
import com.jf.cloud.multishop.dto.ShopDetailDTO;
import com.jf.cloud.multishop.dto.ShopSearchDTO;
import com.jf.cloud.multishop.model.ShopRenovation;
import com.jf.cloud.multishop.service.ShopDetailService;
import com.jf.cloud.multishop.service.ShopRenovationService;
import com.jf.cloud.multishop.vo.ShopDetailAppVO;
import com.jf.cloud.multishop.vo.ShopHeadInfoVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 店铺详情
 *
 * @author zz
 * @date 2020-12-05 15:50:25
 */
@RestController("appShopDetailController")
@RequestMapping("/ua/shop_detail")
@Tag(name = "app-店铺详情")
public class ShopDetailController {

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopRenovationService shopRenovationService;

    @GetMapping("/page")
    @Operation(summary = "搜索店铺列表")
    public ServerResponseEntity<PageVO<ShopDetailAppVO>> page(@Valid PageDTO pageDTO, @ParameterObject ShopDetailDTO shopDetailDTO) {
        return ServerResponseEntity.success(shopDetailService.shopSearchPage(pageDTO, shopDetailDTO));
    }

    @GetMapping("/searchNearShop")
    @Operation(summary = "搜索附近同类店铺列表")
    public ServerResponseEntity<List<ShopDetailAppVO>> searchNearShop(@ParameterObject ShopDetailDTO shopDetailDTO) {
        return ServerResponseEntity.success(shopDetailService.shopSearchNear(shopDetailDTO));
    }

    @GetMapping("/head_info")
    @Operation(summary = "店铺头部信息" , description = "获取的店铺头部信息")
    public ServerResponseEntity<ShopHeadInfoVO> getShopHeadInfo(@ParameterObject Long shopId) {
        ShopHeadInfoVO shopHeadInfoDto = new ShopHeadInfoVO();
        ShopDetailVO shopDetail = shopDetailService.getShoExtensionsByShopId(shopId);
        if(Objects.isNull(shopDetail)){
            return ServerResponseEntity.showFailMsg("店铺不存在！");
        }
        shopHeadInfoDto.setShopStatus(shopDetail.getShopStatus());
        shopHeadInfoDto.setContractStartTime(shopDetail.getContractStartTime());
        shopHeadInfoDto.setContractEndTime(shopDetail.getContractEndTime());
        if(!Objects.equals(shopDetail.getShopStatus(),1)){
            return ServerResponseEntity.success(shopHeadInfoDto);
        }
        ShopRenovation shopHomeRenovation = shopRenovationService.getShopHomeRenovation(shopId, RenovationType.H5.value());
        shopHeadInfoDto.setRenovationId(Objects.nonNull(shopHomeRenovation) ? shopHomeRenovation.getRenovationId() : null);
        shopHeadInfoDto.setShopId(shopId);
        shopHeadInfoDto.setType(shopDetail.getType());
        shopHeadInfoDto.setIntro(shopDetail.getIntro());
        shopHeadInfoDto.setShopLogo(shopDetail.getShopLogo());
        shopHeadInfoDto.setShopName(shopDetail.getShopName());
        shopHeadInfoDto.setIsPreferred(shopDetail.getIsPreferred());
        shopHeadInfoDto.setCollectionNum(shopDetail.getCollectionNum());
        shopHeadInfoDto.setPcBackgroundPic(shopDetail.getPcBackgroundPic());
        shopHeadInfoDto.setMobileBackgroundPic(shopDetail.getMobileBackgroundPic());
        shopHeadInfoDto.setShopLat(shopDetail.getShopLat());
        shopHeadInfoDto.setNoticeMobile(shopDetail.getContactPhone());
        shopHeadInfoDto.setShopLng(shopDetail.getShopLng());
        shopHeadInfoDto.setCity(shopDetail.getCity());
        shopHeadInfoDto.setArea(shopDetail.getArea());
        shopHeadInfoDto.setDetailAddress(shopDetail.getDetailAddress());
        return ServerResponseEntity.success(shopHeadInfoDto);
    }

    @GetMapping("/search_shops")
    @Operation(summary = "app搜索店铺" , description = "根据店铺名称搜索店铺")
    public ServerResponseEntity<PageVO<ShopHeadInfoVO>> searchShops(PageDTO pageDTO, @ParameterObject ShopSearchDTO shopSearchDTO) {
        PageVO<ShopHeadInfoVO> shopPage = shopDetailService.renovationShopPage(pageDTO, shopSearchDTO, SysTypeEnum.ORDINARY.value());
        return ServerResponseEntity.success(shopPage);
    }

    @GetMapping("/list_renovation_shop")
    @Operation(summary = "获取装修店铺列表信息" , description = "获取装修店铺列表信息")
    public ServerResponseEntity<List<ShopHeadInfoVO>> listRenovationShop(@ParameterObject ShopSearchDTO shopSearchDTO) {
        List<ShopHeadInfoVO> shopList = shopDetailService.listRenovationShop(shopSearchDTO);
        return ServerResponseEntity.success(shopList);
    }
}
